function [filtdata]  = filterECG256Hz(data)

% [filtdata]  = filterECG256Hz(data);
% apply a zerophase FIR filter (coeffs designed with sptool)
% Least Sq. FIR LP & HP filters (cascaded): 70dB 0.05-40Hz 1dB ripple. 
% Sampling Frequency of Fs = 256.
%
% Should work nicely for 16bit, 256Hz ECG. You may experince 
% some ringing at turning points, but that's FIR filters for you.  
% You might like to try wavelets for a better filter 
%
% These routines are made available under the GNU general public license. 
% If you have not received a copy of this license, please download from 
% http://www.gnu.org/
%
% Please distribute (and modify) freely, commenting where you have 
% added modifications. The author would appreciate correspondence 
% regarding corrections, modifications, improvements etc.
%
% G. Clifford : gari@ieee.org

% sampling frequency
Fs      = 256;  % Hz    

% Data should be zeromeaned before calling this function
data = data-mean(data);

% set up transfer function
denominator = 1;
numerator_lp =[  
       0.00116294818892327
        0.0108214392935653
        0.0200343666593651
        0.0261187881634098
        0.0212816864030144
       0.00490488877763669
       -0.0145498515998796
       -0.0232788926070469
       -0.0124182936123568
        0.0126862162067748
        0.0328393141039891
        0.0277014738094466
      -0.00663354198352208
       -0.0487259025516825
       -0.0610853025450089
       -0.0144549204767754
        0.0886474308596396
         0.208814361863424
         0.289332487838881
         0.289332487838881
         0.208814361863424
        0.0886474308596396
       -0.0144549204767754
       -0.0610853025450089
       -0.0487259025516825
      -0.00663354198352208
        0.0277014738094466
        0.0328393141039891
        0.0126862162067748
       -0.0124182936123568
       -0.0232788926070469
       -0.0145498515998796
       0.00490488877763669
        0.0212816864030144
        0.0261187881634098
        0.0200343666593651
        0.0108214392935653
       0.00116294818892327];

numerator_hp = [ 
        -0.007719806995443
     -0.000593529464503111
     -0.000616537270116216
     -0.000639451830525362
     -0.000663561635529363
     -0.000687428482340838
     -0.000712345353053305
     -0.000737050063062293
     -0.000762827770843517
     -0.000788276159757994
     -0.000814687362552657
     -0.000840693466317883
     -0.000867737622092878
      -0.00089438708671706
      -0.00092216902638079
     -0.000949570338620629
     -0.000978288661010672
      -0.00100668160625124
      -0.00103659671861193
      -0.00106615479623852
      -0.00109736040348402
      -0.00112792836635087
      -0.00116004251486604
      -0.00119092382531205
      -0.00122322204449099
      -0.00125354739873849
      -0.00128545618449105
      -0.00131494126741691
      -0.00134807599742105
      -0.00138005083638085
      -0.00142229273289703
      -0.00145905623888122
      -0.00147988572401479
      -0.00152337012050789
      -0.00155509077331504
       -0.0015924435019478
      -0.00162645093636032
      -0.00166360336918217
      -0.00169886271207229
      -0.00173643201108726
      -0.00177256853414649
      -0.00181053087910452
      -0.00184738932024739
      -0.00188580324032983
      -0.00192320422309985
      -0.00196187823931274
      -0.00199959655075123
      -0.00203849112299403
       -0.0020765587432622
      -0.00211583028869268
      -0.00215442956472851
      -0.00219430023605191
      -0.00223352552719682
      -0.00227395565365889
      -0.00231354279813875
        -0.002354108014004
      -0.00239338505355968
      -0.00243353302728908
      -0.00247248705230046
      -0.00251352618627335
      -0.00255409536944302
      -0.00259725994910034
      -0.00263509464305156
      -0.00267536483006731
      -0.00271826154742389
      -0.00275770700056424
      -0.00279927582669419
      -0.00283951499715645
      -0.00288073484890323
      -0.00292129482201999
      -0.00296233875603428
      -0.00300290150021605
      -0.00304386170207064
      -0.00308454857127824
      -0.00312549340035217
      -0.00316610295239397
      -0.00320679774729835
       -0.0032471413328337
      -0.00328750802064282
      -0.00332753344802601
      -0.00336758426155901
      -0.00340736140579608
      -0.00344722643493981
      -0.00348685648439877
      -0.00352656997497068
      -0.00356591121574338
      -0.00360510516351791
      -0.00364360588016825
      -0.00368197568241012
      -0.00371996751152801
      -0.00375850930844281
      -0.00379655172798984
      -0.00383430839616961
      -0.00387004946444441
      -0.00390808220759818
      -0.00394482515495904
      -0.00398066969639526
      -0.00401692280951308
      -0.00405221772052505
      -0.00408773437155447
      -0.00412235016462235
      -0.00415686418023859
      -0.00419066438491442
      -0.00422445487363778
       -0.0042576163393507
      -0.00429061997144701
      -0.00432291749710377
      -0.00435498997501222
      -0.00438636173060546
      -0.00441745251390048
      -0.00444780506237289
      -0.00447784887049958
      -0.00450718961782266
      -0.00453627650609555
      -0.00456472176460446
       -0.0045929170622211
      -0.00462034686518763
      -0.00464732123257699
       -0.0046733867508092
      -0.00469915525083144
      -0.00472429941522134
      -0.00474929514447324
      -0.00477312604512227
      -0.00479636638243965
      -0.00481865942392797
      -0.00484177134432678
      -0.00486283917051695
       -0.0048838439863828
      -0.00490407570971269
      -0.00492386545165352
      -0.00494290018123188
       -0.0049611304645503
      -0.00497862211466973
      -0.00499556750358641
      -0.00501186356380093
      -0.00502752874153267
      -0.00504241401877351
      -0.00505665927887701
      -0.00507016713131965
      -0.00508305015409715
      -0.00509513775950472
      -0.00510653645526546
      -0.00511710891690786
      -0.00512701904388992
      -0.00513616850829649
      -0.00514472125325565
      -0.00515250765464019
      -0.00515958960068915
      -0.00516579676427969
      -0.00517132876190234
      -0.00517617853236731
      -0.00518043811157794
      -0.00518382726123044
      -0.00518621003962965
      -0.00518794262187708
      -0.00518900154305289
         0.994810133061056
      -0.00518900154305289
      -0.00518794262187708
      -0.00518621003962965
      -0.00518382726123044
      -0.00518043811157794
      -0.00517617853236731
      -0.00517132876190234
      -0.00516579676427969
      -0.00515958960068915
      -0.00515250765464019
      -0.00514472125325565
      -0.00513616850829649
      -0.00512701904388992
      -0.00511710891690786
      -0.00510653645526546
      -0.00509513775950472
      -0.00508305015409715
      -0.00507016713131965
      -0.00505665927887701
      -0.00504241401877351
      -0.00502752874153267
      -0.00501186356380093
      -0.00499556750358641
      -0.00497862211466973
       -0.0049611304645503
      -0.00494290018123188
      -0.00492386545165352
      -0.00490407570971269
       -0.0048838439863828
      -0.00486283917051695
      -0.00484177134432678
      -0.00481865942392797
      -0.00479636638243965
      -0.00477312604512227
      -0.00474929514447324
      -0.00472429941522134
      -0.00469915525083144
       -0.0046733867508092
      -0.00464732123257699
      -0.00462034686518763
       -0.0045929170622211
      -0.00456472176460446
      -0.00453627650609555
      -0.00450718961782266
      -0.00447784887049958
      -0.00444780506237289
      -0.00441745251390048
      -0.00438636173060546
      -0.00435498997501222
      -0.00432291749710377
      -0.00429061997144701
       -0.0042576163393507
      -0.00422445487363778
      -0.00419066438491442
      -0.00415686418023859
      -0.00412235016462235
      -0.00408773437155447
      -0.00405221772052505
      -0.00401692280951308
      -0.00398066969639526
      -0.00394482515495904
      -0.00390808220759818
      -0.00387004946444441
      -0.00383430839616961
      -0.00379655172798984
      -0.00375850930844281
      -0.00371996751152801
      -0.00368197568241012
      -0.00364360588016825
      -0.00360510516351791
      -0.00356591121574338
      -0.00352656997497068
      -0.00348685648439877
      -0.00344722643493981
      -0.00340736140579608
      -0.00336758426155901
      -0.00332753344802601
      -0.00328750802064282
       -0.0032471413328337
      -0.00320679774729835
      -0.00316610295239397
      -0.00312549340035217
      -0.00308454857127824
      -0.00304386170207064
      -0.00300290150021605
      -0.00296233875603428
      -0.00292129482201999
      -0.00288073484890323
      -0.00283951499715645
      -0.00279927582669419
      -0.00275770700056424
      -0.00271826154742389
      -0.00267536483006731
      -0.00263509464305156
      -0.00259725994910034
      -0.00255409536944302
      -0.00251352618627335
      -0.00247248705230046
      -0.00243353302728908
      -0.00239338505355968
        -0.002354108014004
      -0.00231354279813875
      -0.00227395565365889
      -0.00223352552719682
      -0.00219430023605191
      -0.00215442956472851
      -0.00211583028869268
       -0.0020765587432622
      -0.00203849112299403
      -0.00199959655075123
      -0.00196187823931274
      -0.00192320422309985
      -0.00188580324032983
      -0.00184738932024739
      -0.00181053087910452
      -0.00177256853414649
      -0.00173643201108726
      -0.00169886271207229
      -0.00166360336918217
      -0.00162645093636032
       -0.0015924435019478
      -0.00155509077331504
      -0.00152337012050789
      -0.00147988572401479
      -0.00145905623888122
      -0.00142229273289703
      -0.00138005083638085
      -0.00134807599742105
      -0.00131494126741691
      -0.00128545618449105
      -0.00125354739873849
      -0.00122322204449099
      -0.00119092382531205
      -0.00116004251486604
      -0.00112792836635087
      -0.00109736040348402
      -0.00106615479623852
      -0.00103659671861193
      -0.00100668160625124
     -0.000978288661010672
     -0.000949570338620629
      -0.00092216902638079
      -0.00089438708671706
     -0.000867737622092878
     -0.000840693466317883
     -0.000814687362552657
     -0.000788276159757994
     -0.000762827770843517
     -0.000737050063062293
     -0.000712345353053305
     -0.000687428482340838
     -0.000663561635529363
     -0.000639451830525362
     -0.000616537270116216
     -0.000593529464503111
        -0.007719806995443 ];


% low pass filter
aff_lp = filtfilt(numerator_lp,denominator,data);

% Don't high pass filter it if you don't want to remove the baseline 
% fluctuations due to resp, BP? and electrode noise?
filtdata = filtfilt(numerator_hp,denominator,aff_lp);

% correct for amplitude distortion. !!!??????
%mean_s = mean(new_data);
%mean_a = mean(aff_hp);
%a = aff*(mean_s/mean_a);

